import { _decorator, Component, Node, ButtonComponent, Prefab, Color, instantiate, Vec3, math } from "cc";
import { Notifications } from "../easyFramework/notifications";
import GDef from "../config/GDef";
const { ccclass, property } = _decorator;

@ccclass("BaseView")
export default class BaseView extends Component {
    private _clickEnable: boolean = true; // 控制能否点击的变量
    /**
	 * 添加事件
	 * @param {string} eKey     识别key
	 * @param {object} eCall    回调函数
	 * @param {object} self     识别对象
	 */
     protected on(eKey, eCall, self?) {
        Notifications.on(eKey, eCall, self ? self : this);
    }
    protected off(eKey, eCall, self?) {
        Notifications.off(eKey, eCall, self ? self : this);
    }
    /**
	 * 通知事件
	 * @param {string} eKey     识别key
	 * @param {object} data     传值
	 * @param {object} data2     传值
	 */
    protected emit(eKey,...parameters:any[]) {
        let a = Notifications.emit(eKey,...parameters);
        if (a) {
            return a;
        }
    }
    /**
     * 绑定按钮点击事件,永久监听
     * @param node 挂载了button组件的节点
     * @param callback 回调
     * @param opname 操作内容
     */
    protected bindButton(node: Node, callback: (event?: any) => void, opname?: string = "click"): void {
        let btnComp = node.getComponent(ButtonComponent);
        if (!btnComp) {
            btnComp = node.addComponent(ButtonComponent);
            btnComp.transition = ButtonComponent.Transition.SCALE
        }
        btnComp.zoomScale = 0.9;
        let btnCb = (event) => {
            this.emit(GDef.wdEvent.playEffect,"Button");
            if (!this._clickEnable) return;
            this._clickEnable = false;
            this.scheduleOnce(() => {
                this._clickEnable = true;
            }, 0.3);
            callback.call(this, event);
        };
        node.on(opname, btnCb, this);
    }

     /**
     * 绑定按钮点击事件(只监听一次)
     * @param node 挂载了button组件的节点
     * @param callback 回调
     * @param opname 操作内容
     */
    protected bindButtonOce(node: Node, callback: (event?: any) => void, opname?: string = "click"): void {
        let btnComp = node.getComponent(ButtonComponent);
        if (!btnComp) {
            btnComp = node.addComponent(ButtonComponent);
            btnComp.transition = ButtonComponent.Transition.SCALE
        }
        btnComp.zoomScale = 0.9;
        let btnCb = (event) => {
            this.emit(GDef.wdEvent.playEffect,"Button");
            if (!this._clickEnable) return;
            this._clickEnable = false;
            this.scheduleOnce(() => {
                this._clickEnable = true;
            }, 0.3);
            callback.call(this, event);
        };
        node.once("click", btnCb, this);
    }

    /**
     * 
     * @param url 预制体的路径
     * @param next 回调,返回预制体的主脚本
     */
    // showUI(url, next?) {
    //     MgrView.showUI(url, next);
    // }
    // protected setBtnEnable(btnNode: Node, isEnable: boolean) {
    //     let btnComp = btnNode.getComponent(ButtonComponent);
    //     if (btnComp) {
    //         btnComp.enabled = isEnable;
    //         if (isEnable) {
    //             btnNode.color = Color.WHITE;
    //         } else btnNode.color = Color.GRAY;
    //     } else console.trace("BaseView setBtnEnable, no btn comp");
    // }

    // protected closeSelf() {
    //     AD.clearBanner();
    //     AD.destroyBanner();
    //     console.log('关闭了什么界面',this.node.name)
    //     this.node.destroy();
    // }

    /**
     * 震动
     */
    // protected shock() {
    //     if (WD.isOpenShake() && ChannelMgr.isWechatGame()) {
    //         console.log('震动')
    //         ChannelMgr.vibrateLong();
    //     }
    // }

    onDestroy() {
        Notifications.offTarget(this);
    }

    /**
     * 加载预制体，UI都用这个方法
     * @param cPrefab 要生成的预制体
     * @param cNode 父节点
     * @param next 返回预制体的主脚本
     */
    showUI(cPrefab: Prefab, cNode: Node, next?) {
        let node: Node = instantiate(cPrefab);
        node.parent = cNode;
        next && next(node.getComponent(node.name) ? node.getComponent(node.name) : node);
    }

}
